﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows;
using BGM.Domain.DTO;
using BGM.Sensors.MeasuringDevice;


namespace BloodGlucoseManagerApplication
{
    public partial class UploadMeterWindow
    {
        private Thread uploadThread;
        private List<Reading> data = null;
        SynchronizationContext synchronizationContext;
        IMeasuringDevice device= null;

        public UploadMeterWindow(IMeasuringDevice device)
        {
            this.device = device;
            this.InitializeComponent();

            synchronizationContext = System.Threading.SynchronizationContext.Current;

            uploadThread = new Thread(new ParameterizedThreadStart(UploadDevice));
            uploadThread.IsBackground = true;
            
        }

        private void UploadDevice(object startParm)
        {
            //device = new PrecisionLinkDriver(portName);
            device.MessageRecieved += new MessageRecievedEventhandler(device_MessageRecieved);
           
            synchronizationContext.Post(new SendOrPostCallback(
             delegate
             {
                 labelStatus.Content = "Receiving data...";
             }), null);
        
           

         data=   device.GetReadings(startParm.ToString());

         synchronizationContext.Post(new SendOrPostCallback(
        delegate
        {
            labelStatus.Content = "Transfere complete";
            progressBar1.Value = progressBar1.Maximum;
        }), null);
    
            //OnUploadCompleted(new EventArgs());


            //device.Dispose();

        }

        void device_MessageRecieved(object sender, MessageRecievedEventArgs e)
        {
            synchronizationContext.Post(new SendOrPostCallback(
             delegate
             {
                 labelStatus.Content = e.Message;
                 if (progressBar1.Value < progressBar1.Maximum)
                     progressBar1.Value++;
             }), null);
        }
        void deviceUploader_ConnectionError(object sender, EventArgs e)
        {
            synchronizationContext.Post(new SendOrPostCallback(
                delegate
                {
                    labelStatus.Content = "Connection Error";
                    buttonUpload.IsEnabled = true;
                    comboBoxPort.IsEnabled = true;
                }), null);
        }
      
        public List<Reading> Readings
        {
            get { return data; }

        }
        

        #region Window
        private void buttonUpload_Click(object sender, RoutedEventArgs e)
        {
            buttonUpload.IsEnabled = false;
            comboBoxPort.IsEnabled = false;
            labelStatus.Content = "";


            uploadThread.Start(comboBoxPort.SelectedItem.ToString());
            //deviceUploader = new PrecisionLinkProxy(comboBoxPort.SelectedItem.ToString());
            //deviceUploader.Progress += new PrecisionLinkProxy.ProgressEventHandler(deviceUploader_Progress);
            //deviceUploader.ConnectionError += new EventHandler(deviceUploader_ConnectionError);
            //deviceUploader.UploadCompleted += new EventHandler(deviceUploader_UploadCompleted);
            //deviceUploader.StartAsyncUpload();
        }

       

        private void Window_Initialized(object sender, EventArgs e)
        {
            foreach (string var in System.IO.Ports.SerialPort.GetPortNames())
            {
                comboBoxPort.Items.Add(var);
            }
            if (comboBoxPort.Items.Count > 0)
                comboBoxPort.SelectedIndex = 0;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        #endregion
    }
}